package com.bw.goods_module.bind;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.goods_module.category.CategoryAdapter;
import com.bw.goods_module.category.CategoryEntity;
import com.bw.goods_module.category.CategoryValueAdapter;
import com.bw.goods_module.category.CategoryValueEntity;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.List;

public class CategoryBind {

    @BindingAdapter(value = {"category","clickListener"})
    public static void bindCategory(RecyclerView recyclerView,
                                    List<CategoryEntity.Value> category
            , OnItemClickListener clickListener){
        CategoryAdapter adapter = (CategoryAdapter) recyclerView.getAdapter();
        if (adapter == null){
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            adapter = new CategoryAdapter();
            adapter.setOnItemClickListener(clickListener);
            recyclerView.setAdapter(adapter);
        }else {
            LogUtils.e("bindCategory:"+category.size());
            adapter.setNewInstance(category);
        }
    }

    @BindingAdapter(value = {"categoryValue","clickListener"})
    public static void bindCategoryValue(RecyclerView recyclerView,
                                         List<CategoryValueEntity.Values> categoryValue,
                                         OnItemClickListener clickListener){
        CategoryValueAdapter adapter = (CategoryValueAdapter) recyclerView.getAdapter();
        if (adapter == null){
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            adapter = new CategoryValueAdapter();
            adapter.setOnItemClickListener(clickListener);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.setNewInstance(categoryValue);
        }
    }

}
